# -*- coding: utf-8 -*-
import sys
import redis
import telnetlib
import urllib2 as request
from bs4 import BeautifulSoup

#r = redis.Redis(host='127.0.0.1', port=6379, password='b8dheosn7gf99skb')
#print(1,2,3)
reload(sys)
sys.setdefaultencoding('utf8')
for d in range(1, 100):

    try:
        # print(3,4,5)
        scrapeUrl = 'https://www.kuaidaili.com/free/inha/%d' % d
        print(scrapeUrl)
        req = request.Request(scrapeUrl)
        req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
        req.add_header('Cookie',
                       'channelid=0; sid=1599709803461240; _ga=GA1.2.1671537047.1599710411; _gid=GA1.2.230215376.1599710411; Hm_lvt_7ed65b1cc4b810e9fd37959c9bb51b31=1599710411; _gat=1; Hm_lpvt_7ed65b1cc4b810e9fd37959c9bb51b31=1599717693')
        response = request.urlopen(req)
        html = response.read()
        # print(html)

        bsObj = BeautifulSoup(html, "html.parser")
        trs = bsObj.select('table tbody tr')

        for tr in trs:
            # speed = float(bsObj.select('td')[6 + i * 10].div.get('title').replace('秒', ''))
            td = tr.select('td')
            # print(td)
            speed = td[5].get_text()
            speed = float(speed.replace('秒', ''))
            print(speed)
            if speed < 1:  # 验证速度，只要速度在1秒之内的
                ip = td[0].get_text()
                port = td[1].get_text()
                ip_address = 'http://' + ip + ':' + port
                print(ip, port)
                try:
                    telnetlib.Telnet(ip, port=port, timeout=2)  # 用telnet对ip进行验证
                except:
                    print ('fail')
                else:
                    print ('sucess：' + ip_address)
                    r.sadd('ippool', ip_address)  # 可用的ip导入到redis
                    f = open('proxy_list.txt', 'a')
                    f.write(ip_address + '\n')
                    f.close()
    except:
        print('jj')
        print(sys.exc_info()[0])
    else:
        print('gg')
